import { legacy_createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import { CLEARBTN, INPSHOP, NEXTBTN, PRVEBTN, ALLINPSHOP, STORETYPE, TYPELIST, SHOPLIST, INDEXTAB, ALLTYPE, ALLLIST, PROPSTYPE } from '../Type/Store.d';


const initialState = {
    list: JSON.parse(localStorage.getItem('list') as any) || [],
    childrenList: JSON.parse(localStorage.getItem('childrenList') as any) || [],
    shopList: JSON.parse(localStorage.getItem('shopList') as any) || [],
    CopyList: [],
    AllInpFlag: JSON.parse(localStorage.getItem('AllInpFlag') as any) || false,
    SumPrice: JSON.parse(localStorage.getItem('SumPrice') as any) || 0,
}

const routes = (state: STORETYPE = initialState, { type, payload }: ALLTYPE) => {

    const Set_Item = (name: string, con: PROPSTYPE[] | boolean | number) => {
        window.localStorage.setItem(name, JSON.stringify(con))
    }

    const Sum_Price = () => {
        let sum = 0;
        state.shopList.forEach((item, index) => {
            if (item.flag) sum += item.count * item.price
        })
        state.SumPrice = sum;
        Set_Item('SumPrice', state.SumPrice)
    }

    switch (type) {

        case ALLLIST:
            state.list = payload;
            state.childrenList = payload[0].children as PROPSTYPE[];
            state.CopyList = payload[0].children as PROPSTYPE[];
            Set_Item('list', state.list)
            Set_Item('childrenList', state.childrenList)

            return { ...state }

        case INDEXTAB:
            state.childrenList = state.list[payload].children as PROPSTYPE[];
            state.CopyList = state.list[payload].children as PROPSTYPE[];
            Set_Item('childrenList', state.childrenList)
            return { ...state }

        case SHOPLIST:
            let flag = state.shopList.some(item => item.id == payload.id);
            if (flag) {
                state.shopList.forEach((item, index) => {
                    if (item.id == payload.id) item.count++;
                })
            } else {
                payload.count = 1;
                state.shopList.unshift(payload);
            }
            Set_Item('shopList', state.shopList)
            return { ...state }

        case TYPELIST:
            let typeCopy = [...state.childrenList];
            typeCopy = state.CopyList.filter(item => item.type == payload);
            state.childrenList = typeCopy

            return { ...state }

        case INPSHOP:
            let inpCopy = [...state.shopList];
            inpCopy.forEach((item, index) => {
                if (item.id == payload) item.flag = !item.flag;
            })
            state.AllInpFlag = inpCopy.every(item => item.flag);
            state.shopList = inpCopy;
            Sum_Price()
            Set_Item('shopList', state.shopList)
            Set_Item('AllInpFlag', state.AllInpFlag)
            return { ...state }

        case ALLINPSHOP:
            state.AllInpFlag = payload;
            let allinpCopy = [...state.shopList];
            allinpCopy.forEach((item, index) => {
                item.flag = payload;
            })
            state.shopList = allinpCopy;
            Sum_Price()
            Set_Item('shopList', state.shopList)
            Set_Item('AllInpFlag', state.AllInpFlag)
            return { ...state }

        case PRVEBTN:
            let prevCopy = [...state.shopList];
            prevCopy.forEach((item, index) => {
                if (item.id == payload) {
                    if (item.count == 1) return;
                    item.count--
                }
            })
            state.shopList = prevCopy;
            Sum_Price()
            Set_Item('shopList', state.shopList)
            return { ...state }

        case NEXTBTN:
            let nextCopy = [...state.shopList];
            nextCopy.forEach((item, index) => {
                if (item.id == payload) {
                    item.count++
                }
            })
            state.shopList = nextCopy;
            Sum_Price()
            Set_Item('shopList', state.shopList)
            return { ...state }

        case CLEARBTN:
            let clearCopy = [...state.shopList];
            for (let i = clearCopy.length - 1; i > -1; i--) {
                clearCopy.forEach((item, index) => {
                    if (item.flag) clearCopy.splice(index, 1)
                })
            }

            state.shopList = clearCopy;
            state.AllInpFlag = false;
            Sum_Price()
            Set_Item('AllInpFlag', state.AllInpFlag)
            Set_Item('shopList', state.shopList)
            return { ...state }

        // case INDEXTAB:
        //     return { ...state}

        // case INDEXTAB:
        //     return { ...state}
        default:
            return state
    }
}

export default legacy_createStore(routes, applyMiddleware(thunk, logger))
